using Circus.Syntax.Design.Dialogs;
using Circus.Syntax.Lexer;
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace Circus.Syntax.Design
{
	public class SyntaxBuilderEditor : UITypeEditor
	{
		private IWindowsFormsEditorService service = null;
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			object result;
			if (context == null || context.Instance == null || provider == null)
			{
				result = value;
			}
			else
			{
				this.service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
				if (this.service == null)
				{
					result = value;
				}
				else
				{
					DlgSyntaxBuilder dlgSyntaxBuilder = new DlgSyntaxBuilder(this);
					try
					{
						dlgSyntaxBuilder.Scheme = (LexScheme)value;
					}
					catch (Exception ex)
					{
						MessageBox.Show("Not implemented: Error loading scheme:" + ex.ToString());
						dlgSyntaxBuilder.Dispose();
						this.service = null;
						result = value;
						return result;
					}
					DialogResult dialogResult = this.service.ShowDialog(dlgSyntaxBuilder);
					if (dialogResult == DialogResult.OK)
					{
						value = dlgSyntaxBuilder.Scheme;
						((LexScheme)value).Update();
						context.OnComponentChanged();
					}
					dlgSyntaxBuilder.Dispose();
					this.service = null;
					result = value;
				}
			}
			return result;
		}
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			UITypeEditorEditStyle result;
			if (context != null && context.Instance != null)
			{
				result = UITypeEditorEditStyle.Modal;
			}
			else
			{
				result = base.GetEditStyle(context);
			}
			return result;
		}
	}
}
